package com.study.study.pool;

import cn.hutool.core.thread.NamedThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 名称：SchedulerTest
 * 功能描述：
 *
 * @author: zhang
 * @version: 3.0
 * @date: 2019-09-09 21:12:22
 * @since 1.8
 **/
public class SchedulerTest {
    private static final Logger LOG = LoggerFactory.getLogger(SchedulerTest.class);

    public static void main(String[] args) {
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor =
                new ScheduledThreadPoolExecutor(2, new NamedThreadFactory("load-pool", false));
//        scheduledThreadPoolExecutor.scheduleWithFixedDelay(()->{
//            try {
//                Thread.sleep(60L);
//                LOG.info("run...");
//                throw new RuntimeException("dsadadsa");
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }, 0, 5000, TimeUnit.MILLISECONDS);
//        scheduledThreadPoolExecutor.scheduleWithFixedDelay(()->{
//            try {
//                Thread.sleep(40L);
//            LOG.info("run2...");
//                throw new RuntimeException("dsadadsa");
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }, 0, 5000, TimeUnit.MILLISECONDS);


        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor2 =
                new ScheduledThreadPoolExecutor(2, new NamedThreadFactory("load-pool", false));
        scheduledThreadPoolExecutor2.scheduleAtFixedRate(() -> {
            try {
                Thread.sleep(60L);
                LOG.info("run...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            throw new RuntimeException();
        }, 1000, 50, TimeUnit.MILLISECONDS);

        scheduledThreadPoolExecutor2.scheduleAtFixedRate(() -> {
            try {
                Thread.sleep(70L);
                LOG.info("run2...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            throw new RuntimeException();
        }, 1000, 50, TimeUnit.MILLISECONDS);
    }
}
